import sys
import time

from PySide6 import QtWidgets
from PySide6.QtCore import QRectF, Qt, QThread, Signal
from PySide6.QtGui import QFont, QResizeEvent, QPainter, QPen, QConicalGradient, QColor
from PySide6.QtWidgets import QFrame


class CircularProgressBar(QFrame):
    def __init__(self,work):
        super().__init__()
        self.progress = 0
        self.text = ''
        self.win_size = self.size()
        self.resize(100,100)
        self.font = QFont('微软雅黑', 12)
        self.setWindowFlags(Qt.FramelessWindowHint)  # 窗口无边框
        self.setAttribute(Qt.WA_TranslucentBackground)  # 窗口透明

        self.work = work
        self.run_work()
    def run_work(self):
        # 创建线程
        # 连接信号
        self.work._signal.connect(self.call_backlog)  # 进程连接回传到GUI的事件
        # 开始线程
        self.work.start()

    def call_backlog(self, msg):
        self.setValue(int(msg))  # 将线程的参数传入进度条

    def setValue(self, n: int):
        if self.progress == n:
            return
        else:
            self.progress = n
            self.update()

    def setFont(self, font: QFont) -> None:
        self.font = font
        self.update()
        super().setFont(font)

    def setText(self, text):
        self.text = text
        self.update()

    def resizeEvent(self, evt: QResizeEvent) -> None:
        self.win_size = evt.size()
        super().resizeEvent(evt)

    def paintEvent(self, e):
        try:
            self.resize(self.win_size)
            pd = int(self.progress / 100 * 360)
            painter = QPainter(self)
            # 填充
            # painter.fillRect(self.rect(), Qt.white)
            painter.translate(10, 10)
            painter.setRenderHint(QPainter.Antialiasing)
            # 圆坐标
            circle_rect = QRectF(self.rect().left() / 2, self.rect().top() / 2, self.width() - 20, self.height() - 20)
            painter.setPen(QPen(QColor(173, 173, 173, 150), 3))
            painter.drawEllipse(circle_rect)  # 画内圆
            # 渐变色
            gradient = QConicalGradient(50, 50, 0)
            gradient.setColorAt(0.2, QColor('#12c2e9'))
            gradient.setColorAt(0.4, QColor('#c471ed'))
            gradient.setColorAt(0.6, QColor('#f64f59'))
            gradient.setColorAt(0.8, QColor('#c471ed'))
            gradient.setColorAt(1, QColor('#12c2e9'))
            # 外圈圆
            pen = QPen()
            pen.setCapStyle(Qt.RoundCap)  # 设置圆帽
            pen.setWidth(8)  # 笔宽度
            pen.setBrush(gradient)
            painter.setPen(pen)
            painter.drawArc(circle_rect, (90 - 0) * 16, int(-pd * 16))  # 画圆环

            # 设置字体
            painter.setFont(self.font)
            sizeF = self.font.pointSizeF() + 10
            text_width = (len(self.text) + 4) * sizeF
            # 设置文本大小与宽度
            rectF = QRectF(
                (self.win_size.width() - 20 - text_width) / 2,
                (self.win_size.height() - 20 - sizeF) / 2,
                text_width, sizeF
            )
            painter.drawText(rectF, Qt.AlignCenter, self.text + "%d%%" % int(self.progress))  # 显示进度条当前进度
        except Exception as ee:
            print(ee)
class circularRingBar():
    def __init__(self, work):
        self.app = QtWidgets.QApplication(sys.argv)
        self.myshow = CircularProgressBar(work)
        self.myshow.show()
        sys.exit(self.app.exec())

# 继承QThread
# 你的耗时操作，或者是某个复杂的算法，采样多线程的形式
class Runthread(QThread):
    #  通过类成员对象定义信号对象
    _signal = Signal(int)

    def __init__(self):
        super(Runthread, self).__init__()

    def run(self):
        for i in range(100):
            ##########################
            #                        #
            # 将需要计算的代码块放在此处  #
            #                        #
            ##########################

            time.sleep(0.1)
            self._signal.emit(i + 1)  # 发送实时任务进度和总任务进度
if __name__ == '__main__':

    work = Runthread()
    bar = circularRingBar(work)

